package ncit.bluetooth.settings;

import ncit.bluetooth.control.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class AdvanceSettingsLego extends Activity{
	
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nxt_adv_settings);
		
		ArrayAdapter<CharSequence> adapter_in = ArrayAdapter.createFromResource(
				  this, R.array.in_ports, android.R.layout.simple_spinner_item);
		adapter_in.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
		
		ArrayAdapter<CharSequence> adapter_out = ArrayAdapter.createFromResource(
				  this, R.array.out_ports, android.R.layout.simple_spinner_item);
		adapter_out.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
		
		
		Spinner leftBackMotor = (Spinner) findViewById( R.id.spinner1 );
		leftBackMotor.setAdapter( adapter_out );
		leftBackMotor.setOnItemSelectedListener( new ItemSelectListener("leftBackMotor") );
		leftBackMotor.setSelection(BluetoothControlSettings.integerSettings.get("leftBackMotor"));
		
		Spinner rightBackMotor = (Spinner) findViewById( R.id.spinner2 );
		rightBackMotor.setAdapter( adapter_out );
		rightBackMotor.setOnItemSelectedListener( new ItemSelectListener("rightBackMotor") );
		rightBackMotor.setSelection(BluetoothControlSettings.integerSettings.get("rightBackMotor"));
		
		Spinner directionMotor = (Spinner) findViewById( R.id.spinner3 );
		directionMotor.setAdapter( adapter_out );
		directionMotor.setOnItemSelectedListener( new ItemSelectListener("directionMotor") );	
		directionMotor.setSelection(BluetoothControlSettings.integerSettings.get("directionMotor"));
		
		Spinner leftBackTouch = (Spinner) findViewById( R.id.spinner4 );
		leftBackTouch.setAdapter( adapter_in );
		leftBackTouch.setOnItemSelectedListener( new ItemSelectListener("leftBackTouch") );
		leftBackTouch.setSelection(BluetoothControlSettings.integerSettings.get("leftBackTouch"));
		
		Spinner rightBackTouch = (Spinner) findViewById( R.id.spinner5 );
		rightBackTouch.setAdapter( adapter_in );
		rightBackTouch.setOnItemSelectedListener( new ItemSelectListener("rightBackTouch") );
		rightBackTouch.setSelection(BluetoothControlSettings.integerSettings.get("rightBackTouch"));
		
		Spinner ultrasonic = (Spinner) findViewById( R.id.spinner6 );
		ultrasonic.setAdapter( adapter_in );
		ultrasonic.setOnItemSelectedListener( new ItemSelectListener("ultrasonic") );
		ultrasonic.setSelection(BluetoothControlSettings.integerSettings.get("ultrasonic"));
		
	}
	
	
	/**
	 * Class that links a spinner with the according property from BluetoothControlSettings.integerSettings.  
	 * @author Tony
	 *
	 */
	private class ItemSelectListener implements OnItemSelectedListener{

		private String propertyToModify = "";
		
		public ItemSelectListener(String property){
			
			this.propertyToModify = property;
			
		}
		
		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			
			BluetoothControlSettings.integerSettings.remove(this.propertyToModify);
			BluetoothControlSettings.integerSettings.put(this.propertyToModify, pos);
			
		}

		public void onNothingSelected(AdapterView<?> parent) {
			
			
		}
		
	}
	
	
}
